package com.zicheng;

import com.easyooo.framework.common.dto.SortFast;
import com.easyooo.framework.common.enums.DatePattern;
import com.easyooo.framework.common.enums.SortType;
import com.easyooo.framework.common.util.*;
import com.zicheng.luckyGift.array.ArrayTests;
import com.zicheng.luckyGift.dto.LuckyAwardDTO;
import com.zicheng.luckyGift.dto.StudentDTO;
import com.zicheng.util.DateCommonUtil;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.resps.Tuple;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@SuppressWarnings(value = "all")
public class Main {
    public static final String DEFAULT_GOD_NAME_LEVEL_6 = "檬檬君主#5qqs5qqs5ZCb5Li7";

    public void testPackageLuckyAwardDTO() {
        LuckyAwardDTO one = new LuckyAwardDTO("粉刷匠1", 0);
        LuckyAwardDTO two = new LuckyAwardDTO("粉刷匠1", 0);
        LuckyAwardDTO three = new LuckyAwardDTO("粉刷匠2", 0);
        LuckyAwardDTO four = new LuckyAwardDTO("粉刷匠4", 0);
        List<LuckyAwardDTO> awardDTOList = new ArrayList<>();

        awardDTOList.add(one);
        awardDTOList.add(three);
        awardDTOList.add(two);
        awardDTOList.add(four);
        ArrayTests arrayTests = new ArrayTests();
        arrayTests.testArrayLocation(awardDTOList);

    }

    /**
     * 测试 根据 实体类 中 某个字段值，将实体类分成 Key 字段值 : Value 当前实体类的列表 的格式
     */
    @Test
    public void testListUtilsGrouping() {
        List<StudentDTO> stuDTO = new ArrayList<>();
        StudentDTO s1 = new StudentDTO("z", 10, 1);
        StudentDTO s2 = new StudentDTO("a", 11, 1);
        StudentDTO s3 = new StudentDTO("b", 12, 1);
        StudentDTO s4 = new StudentDTO("c", 13, 2);
        StudentDTO s5 = new StudentDTO("d", 13, 2);
        StudentDTO s6 = new StudentDTO("e", 14, 3);
        stuDTO.add(s1);
        stuDTO.add(s2);
        stuDTO.add(s3);
        stuDTO.add(s4);
        stuDTO.add(s5);
        stuDTO.add(s6);

        Map<Integer, List<StudentDTO>> grouping = ListUtils.grouping(stuDTO, StudentDTO::getGroup);
        for (Map.Entry<Integer, List<StudentDTO>> integerListEntry : grouping.entrySet()) {
            String s = integerListEntry.getKey() + " : " + integerListEntry.getValue();
            System.out.println(s);
        }
    }

    /**
     * NumberUtils.FormatNumber 能够格式化数字，例如10000，可以显示出1w
     */
    @Test
    public void testNumberUtilsFormatNumber() {
        Long num1 = 1000L;
        Long num2 = 1000000L;
        String end = "w";
        System.out.println(NumberUtils.formatNumber(num1, true, end));
        System.out.println(NumberUtils.formatNumber(num1, false, end));
        System.out.println("-----------------------_______________________________");
        System.out.println(NumberUtils.formatNumber(num2, true, end));
        System.out.println(NumberUtils.formatNumber(num2, false, end));

    }

    /**
     * 这应该是一个用于排序的工具类，其中有一个sortFast，它需要传入一个接口，我觉得这是排序规则，把规则抽象成了接口
     */
    @Test
    public void testSortUtilsSortFast() {
        List<StudentDTO> stuDTO = new ArrayList<>();
        StudentDTO s1 = new StudentDTO("z", 19, 1, true);
        StudentDTO s2 = new StudentDTO("a", 11, 1, true);
        StudentDTO s3 = new StudentDTO("b", 12, 1, false);
        StudentDTO s4 = new StudentDTO("c", 13, 2, false);
        StudentDTO s5 = new StudentDTO("d", 13, 2, true);
        StudentDTO s6 = new StudentDTO("e", 14, 3, false);
        stuDTO.add(s1);
        stuDTO.add(s2);
        stuDTO.add(s3);
        stuDTO.add(s4);
        stuDTO.add(s5);
        stuDTO.add(s6);
        SortUtils.sortFast(stuDTO, new SortFast<StudentDTO>() {
            @Override
            public String[] sortField() {
                /*
                 * 按照哪个字段排序
                 * */
                return new String[]{"isMarried"};
            }

            @Override
            public SortType[] sortType() {
                /*
                 * 排序规则
                 * */
                return new SortType[]{SortType.Descending, SortType.Ascending};
            }

            @Override
            public Object sortValue(String s, StudentDTO studentDTO) {
                if ("isMarried".equals(s)) {
                    return studentDTO.isMarried();
                }
                return null;
            }
        });

        for (StudentDTO studentDTO : stuDTO) {
            System.out.print(studentDTO.getName());
            System.out.print("  ");
            System.out.print(studentDTO.getAge());
            System.out.print("  ");
            System.out.print(studentDTO.isMarried());
            System.out.println();
        }
    }

    /**
     * 测试时间解析
     **/
    @Test
    public void testDateTimeUtilsIsSameDay() {
        Date statDate = DateUtils.getFirstOfWeek();
        Date parse = DateUtils.parse("2025-03-24 00:00:00", DatePattern.YMD_HMS);
        System.out.println(DateUtils.isSameDay(parse, statDate));
    }

    @Test
    public void testDateCommonUtils() {
        Date normal = new Date();
        Date w2M = DateUtils.parse("2025-03-02 00:00:00", DatePattern.YMD_HMS);
        Date m2W = DateUtils.parse("2025-05-01 00:00:00", DatePattern.YMD_HMS);
        DateCommonUtil.getStartTime(DateCommonUtil.MaiShiTimeEnum.Week2Month, normal);
        DateCommonUtil.getStartTime(DateCommonUtil.MaiShiTimeEnum.Month2Week, normal);
        DateCommonUtil.getStartTime(DateCommonUtil.MaiShiTimeEnum.Week2Month, w2M);
        DateCommonUtil.getStartTime(DateCommonUtil.MaiShiTimeEnum.Month2Week, w2M);
        DateCommonUtil.getStartTime(DateCommonUtil.MaiShiTimeEnum.Week2Month, m2W);
        DateCommonUtil.getStartTime(DateCommonUtil.MaiShiTimeEnum.Month2Week, m2W);
        Date today = DateUtils.parse(DateUtils.plusDays(new Date(), -1), DatePattern.YMD);
        System.out.println(today);
    }

    @Test
    public void commonTest() {

    }

    @Test
    public void msgTest() {
        String prefix = "咚咚咚~，尊敬的干先生，现在为您准点报时，";
        String unDownMsg = "您还有{}小时{}分{}秒下班";
        String downMsg = "您已经加班{}小时{}分{}秒";
        String msg = "";

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime workDown = LocalDateTime.parse("2025-05-08 17:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        int hour = workDown.getHour() - now.getHour();
        int minute = workDown.getMinute() - now.getMinute();
        int second = workDown.getSecond() - now.getSecond();

        if (second < 0) {
            minute = minute < 0 ? minute++ : minute--;
            second = second + 60;
        }
        if (minute < 0) {
            hour--;
            minute = minute + 60;
        }
        if (hour < 0) {
            msg = prefix + downMsg;
            hour++;
            hour = -hour;
            second = 60 - second;
            minute = 60 - minute;
        } else {
            msg = prefix + unDownMsg;
        }
        System.out.println(MsgUtils.format(msg, hour, minute, second));
    }

    /**
     * 测试这段代码，是如何切割的
     */
    @Test
    public void testSubString() {
        String royalName;
        String[] split = StringUtils.split(DEFAULT_GOD_NAME_LEVEL_6, "#");
        royalName = split[0].substring(0, 2);
        System.out.println(royalName);
    }

    /**
     * 使用Collectors.toMap，存在一些疑惑，测试使用toMap
     */
    @Test
    public void testToMap() {
        Tuple tuple1 = new Tuple("001", 100d);
        Tuple tuple2 = new Tuple("002", 200d);
        Tuple tuple3 = new Tuple("003", 300d);
        Tuple tuple4 = new Tuple("004", 400d);
        Tuple tuple5 = new Tuple("005", 500d);
        List<Tuple> userIdAndScoreList = new ArrayList<>();
        userIdAndScoreList.add(tuple1);
        userIdAndScoreList.add(tuple2);
        userIdAndScoreList.add(tuple3);
        userIdAndScoreList.add(tuple4);
        userIdAndScoreList.add(tuple5);
        HashSet<Long> userIdSet = new HashSet<>();
        List<Map<Long, Long>> mapList = new ArrayList<>();
        mapList.add(toMap(userIdAndScoreList, userIdSet));
        System.out.println("记录日志");
    }

    public Map<Long, Long> toMap(List<Tuple> list, Set<Long> set) {
        list.stream().map((ele) -> Long.valueOf(ele.getElement())).forEach(set::add);

        return list.stream().collect(Collectors.toMap(
                (tuple -> {
                    return Long.parseLong(tuple.getElement());
                })
                , tuple -> {
                    return (long) tuple.getScore();
                }
                , (els, val) -> els));
    }

    /** 使用DateUtils中的方法,对时间工具具体会返回什么时间存在疑问
     * */
    @Test
    public void testDateUtils() {
        Date endOfMonth = DateUtils.getEndOfMonth();
        System.out.printf("end of month:%s\n", endOfMonth);
        Date startOfDay = DateUtils.getStartOfDay(endOfMonth);
        System.out.printf("start of day:%s\n", startOfDay);
        Date eightOfDay = DateUtils.plusHours(endOfMonth, -4);
        System.out.printf("eight of day:%s\n", eightOfDay);
        Date preStart = DateUtils.plusMinutes(eightOfDay, 6);
        String preStr = DateUtils.toString(preStart, DatePattern.YMD_HMS);
        System.out.printf("pre:%s\n", preStr);
        Date preEnd = DateUtils.plusMinutes(eightOfDay, 30);
        String preEndStr = DateUtils.toString(preEnd, DatePattern.YMD_HMS);
        System.out.printf("pre end:%s\n", preEndStr);
        Date matchEnd = DateUtils.plusMinutes(eightOfDay, 35);
        String endStr = DateUtils.toString(matchEnd, DatePattern.YMD_HMS);
        System.out.printf("end end:%s\n", endStr);
    }
}